package com.spzx.product.helper;

import com.spzx.product.api.domain.CategoryVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ChenXing
 * @Description  使用递归方法，构建 tree
 * @Create 2024/8/8 9:44
 */
public class CategoryHelper {


    public static List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {
        List<CategoryVo> trees = new ArrayList<>();
        for (CategoryVo categoryVo : categoryVoList) {
            if (categoryVo.getParentId().longValue() == 0) {
                trees.add(findChildren(categoryVo,categoryVoList));
            }
        }
        return trees;
    }


    public static CategoryVo findChildren(CategoryVo categoryVo, List<CategoryVo> treeNodes) {
        categoryVo.setChildren(new ArrayList<CategoryVo>());

        for (CategoryVo it : treeNodes) {
            if(categoryVo.getId().longValue() == it.getParentId().longValue()) {
                if (categoryVo.getChildren() == null) {
                    categoryVo.setChildren(new ArrayList<>());
                }
                categoryVo.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return categoryVo;
    }
}
